﻿Public Class FIssueNewPairUpTicket

    Public Shadows Sub Show(ByVal owner As IWin32Window)
        ListView1.BeginUpdate()
        For Each clsBuddy As Entities.Buddy In DataStore.Buddies.GetAll
            With ListView1.Items.Add(clsBuddy.Name)
                .Tag = clsBuddy
                If clsBuddy.PairUpValidUntil > Now.ToUniversalTime Then
                    .SubItems.Add("Yes")
                Else
                    .SubItems.Add(String.Empty)
                End If
                .SubItems.Add(clsBuddy.Comment)
            End With
        Next
        ListView1.EndUpdate()
        Me.Icon = CommonResources.My.Resources.Buddy
        MyBase.Show(owner)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim clsBud As Entities.Buddy
        If rbNewBuddy.Checked Then
            clsBud = New Entities.Buddy
            clsBud.Name = txtName.Text
            clsBud.Comment = txtComment.Text
        Else
            If ListView1.SelectedItems.Count <> 1 Then
                MessageBox.Show(Me, "You must select one buddy!", "Reissue ticket", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Return
            End If
            clsBud = CType(ListView1.SelectedItems(0).Tag, Entities.Buddy)
            If Not String.IsNullOrEmpty(clsBud.PassCode_LoginAtBuddy) And Not String.IsNullOrEmpty(clsBud.PassCode_LoginHere) Then
                Select Case MessageBox.Show(Me, String.Format("You are already paired up with {0}! This action will break that pair up connection! Continue?", clsBud.Name), "Already paired up", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
                    Case Windows.Forms.DialogResult.Yes
                    Case Else
                        Return
                End Select
            End If
        End If
        NetNode.Manager.IssuePairupPinCode(clsBud, rbViaEmail.Checked)

        DataStore.Buddies.Save(clsBud)

        Dim clsShowPinCode As New FShowPairUpTicket
        clsShowPinCode.ShowDialog(Me, clsBud)

        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        rbReissue.Checked = True
    End Sub

End Class